package pl.edu.politechnika.zio.qfl.domain.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;

import pl.edu.politechnika.zio.qfl.domain.Message;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;

@Service("messageService")
@RemotingDestination(channels = { "my-amf" })
public class MessageDAO implements IMessageDAO {
    static {
        ObjectifyService.register(Message.class);
    }
    
    private Objectify oby;

    @Autowired
    public MessageDAO(ObjectifyFactory objectifyFactory) {
        this.oby = objectifyFactory.begin();
    }

    @Override
    @RemotingInclude
    public List<Message> findAll() {
        return oby.query(Message.class).list();
    }

    @Override
    @RemotingInclude
    public Message findById(Long id) {
        return oby.find(Message.class, id);
    }

    @Override
    @RemotingInclude
    public Key<Message> create(Message object) {
        return oby.put(object);
    }

    @Override
    @RemotingInclude
    public Key<Message> update(Message object) {
        return create(object);
    }

    @Override
    @RemotingInclude
    public void remove(Message object) {
        oby.delete(object);
    }

    @Override
    @RemotingInclude
    public void removeById(Long id) {
        oby.delete(Message.class, id);
    }

}
